$(function(){
    var slides = $(".slider .slide").length;
    var target = null;
    $(".sliderContainer").hover(function() {
        var selectedItem = $(".slideSelectors div.item.selected");
        var index = -1;
        if (selectedItem != undefined) {
            index = selectedItem.attr("data-index");
        }
        //var index = $(".slideSelectors div.selected").getAttribute("data-index");
        var target = $(".sliderContainer .slideContent");
        // console.debug(index);
        if (index != -1) {
            var slides =  $(".slider .slide");
            var currentSlide = null;
            $.each(slides,function(i,element){
                var dIndex = element.getAttribute("data-index");
                if(dIndex==index){
                    currentSlide = element;
                }
            });
            target.find(".content-title").html("<a href='" + currentSlide.getAttribute("data-url") + "'>" + currentSlide.getAttribute("data-title") + "</a>");
            target.find(".content-desc").html(currentSlide.getAttribute("data-desc"));
            target.fadeIn();
        }

    },function(){
        var target = $(".sliderContainer .slideContent");
        target.fadeOut();
    });

    initSlider();
    if(window.ie6==false){
        initDropdown();
    }

    fitTileHeight();
    
});
function initSlider() {
    $(".iosSlider").iosSlider({
        desktopClickDrag: true,
        snapToChildren: true,
        infiniteSlider: true,
        navSlideSelector: '.sliderContainer .slideSelectors .item',
        onSlideComplete: slideComplete,
        onSliderLoaded: sliderLoaded,
        onSlideChange: slideChange,
        autoSlide: true,
        scrollbar: true,
        scollbarContainer: '.sliderContainer .scrollbarContainer',
        scrollbarMargin: '0',
        scrollbarBorderRadius: '0',
        keyboardControls: true
    });
}

function slideChange(args) {
    $(".sliderContainer .slideSelectors .item").removeClass("selected");
    $(".sliderContainer .slideSelectors .item:eq(" + (args.currentSlideNumber - 1) + ")").addClass("selected");

    slideComplete(args);
}

function slideComplete(args) {
    if (!args.slideChanged) return false;
    var i = args.currentSlideNumber - 1;
    var el = $(".sliderContainer div.slide");
    var target = $(".sliderContainer .slideContent");

    if(window.ie6){
        try{
         fixed_ie6(args);
        }catch(exception){
        }
    }
    target.find(".content-title").html("<a href='" + el[i].getAttribute("data-url") + "'>" + el[i].getAttribute("data-title") + "</a>");
    target.find(".content-desc").html(el[i].getAttribute("data-desc"));
    //target.fadeIn();
    target.hide();

    $(".slideSelectors div.selected").removeClass("selected");
    $(".slideSelectors div").eq(i).addClass("selected");
}

function sliderLoaded(args) {
    slideChange(args);
}
function initDropdown() {
    $("#menu > li").hover(function () {
        $(this).children(".dropdown-menu").slideDown();
        $(this).addClass("open");
    }, function () {
        $(this).children(".dropdown-menu").slideUp();
        $(this).removeClass("open");
    });

    $("ul.nav-pills > li >a").hover(function(){
        $(this).tab("show");
    });
}

function fitTileHeight(){
    $(".tile").hover(function(){
        var $tileback = $(this).find(".tile-back"),
            $tilefront = $(this).find(".tile-front");
        $tileback.height($tilefront.height());
        $tileback.width($tilefront.width());
    });
}

function fixIcons(){
	$(".dialog-container").hover(function(){
		$(this).find(".dialog-content").css("display","block");
	},function(){
		$(this).find(".dialog-content").css("display","none");
	});
}